const { PartPurchase, Part, Category } = require('../common/mysql.js')
const { Op } = require('sequelize')

module.exports = {
    async getList(store_id) {
        return await PartPurchase.findAll({
            include: [
                {
                    model: Part,
                    include: [
                        {
                            model: Category
                        }
                    ]
                }
            ],
            where: {
                store_id: {
                    [Op.eq]: store_id
                }
            }
        })
    },
    async getOne(id, store_id) {
        return await PartPurchase.findOne({
            where: {
                id: {
                    [Op.eq]: id
                },
                store_id: {
                    [Op.eq]: store_id
                }
            }
        })
    },
    async insert(partPurchase) {
        await PartPurchase.create(partPurchase)
    },
    async update(id, store_id, partPurchase) {
        await PartPurchase.update(partPurchase, {
            fields: ['name', 'count', 'date', 'part_id'],
            where: {
                id: {
                    [Op.eq]: id
                },
                store_id: {
                    [Op.eq]: store_id
                }
            }
        })
    },
    async del(id, store_id) {
        await PartPurchase.destroy({
            where: {
                id: {
                    [Op.eq]: id
                },
                store_id: {
                    [Op.eq]: store_id
                }
            }
        })
    }
}
